//
//  SecondViewController.m
//  Follow Me
//
//  Created by apple on 2017/3/9.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "SecondViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "mapView.h"
#import "MainModel.h"
#import "Routes+CoreDataClass.h"
#import "Photos+CoreDataClass.h"
#import "Users+CoreDataClass.h"
#import "Destinations+CoreDataClass.h"


@interface SecondViewController ()<MKMapViewDelegate,CLLocationManagerDelegate,mapViewDelegate>
@property(nonatomic, strong)mapView *map;


@end

@implementation SecondViewController

- (void)viewDidLoad {

    
    [super viewDidLoad];
    
    self.map = [[NSBundle mainBundle] loadNibNamed:@"mapView" owner:self options:nil].firstObject;
    
    [self.view addSubview:self.map];
    
    
    self.map.delegate = self;
    
    

    //设置状态栏前景为白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    self.tabBarController.tabBar.hidden = NO;
    
    [self.navigationController.navigationBar setHidden:YES];
}


-(void)actionOut:(NSString *)value
{
    
       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要保存吗" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //填充Routes
        Routes *routeInfo = [MainModel model].routeMsg;
        routeInfo.distance = self.map.lengthOfRoute.text;
        routeInfo.time = self.map.timeCount.text;
//        NSDate *currentDate = [NSDate date];//获取当前时间，日期
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//        [dateFormatter setDateFormat:@"YYYY-MM-dd"];
//        NSString *dateString = [dateFormatter stringFromDate:currentDate];
        routeInfo.routeDate = [NSDate date];
        routeInfo.mapData = [NSKeyedArchiver archivedDataWithRootObject:self.map.locationArray];
        NSArray *user = [Users findByAttribute:@"userName" withValue:[MainModel model].userMsg.userName];
        Users *userInfo = [user firstObject];
        userInfo.route = [NSSet setWithObjects:routeInfo, nil];
        [MainModel model].routeMsg = routeInfo;
        [[NSManagedObjectContext defaultContext] saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
            NSLog(@"%@",routeInfo);
            
        }];
        
        
        
        //completionHandler();
        [self.map saveOrNot];
        //跳转到EditRouteViewController
        EditRouteViewController *EditRVC = [self.storyboard instantiateViewControllerWithIdentifier:@"EditRoute"];
        [self.navigationController pushViewController:EditRVC animated:YES];
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"不保存" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //completionHandler();
        [self.map saveOrNot];
        Routes *routeNotNeeded = [MainModel model].routeMsg;
        [routeNotNeeded deleteEntity];
        [MainModel model].routeMsg = NULL;
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        //completionHandler();
        Routes *routeNotNeeded = [MainModel model].routeMsg;
        [routeNotNeeded deleteEntity];
        [MainModel model].routeMsg = NULL;
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    
    NSLog(@"%@",value);
}






@end
